LIKE Condition

Course- PostgreSQL >

This PostgreSQL tutorial explains how to use the PostgreSQL LIKE condition to perform pattern matching with syntax and examples.

Description

The PostgreSQL LIKE condition allows wildcards to be used in the WHERE clause of a SELECT, INSERT, UPDATE, or DELETE statement. This allows you to perform pattern matching.

Syntax

The syntax for the LIKE condition in PostgreSQL is:

expression LIKE pattern [ ESCAPE 'escape_character' ]

Parameters or Arguments

expression

A character expression such as a column or field.

pattern

A character expression that contains pattern matching. The patterns that you can choose from are:

Wildcard

Explanation

%

Allows you to match any string of any length (including zero length)

_

Allows you to match on a single character

escape_character

Optional. It allows you to test for literal instances of a wildcard character such as % or _. If you do not provide the escape_character, PostgreSQL assumes that \ is the escape_character.

Example - Using % wildcard (percent sign wildcard)

The first PostgreSQL LIKE example that we will look at involves using the % wildcard (percent sign wildcard).

Let's explain how the % wildcard works in the PostgreSQL LIKE condition. We want to find all of the employees whose first_name begins with 'Jo'.

SELECT *

FROM employees

WHERE first_name LIKE 'Jo%';

You can also using the % wildcard multiple times within the same string. For example,

SELECT *

FROM employees

WHERE first_name LIKE '%od%';

In this PostgreSQL LIKE condition example, we are looking for all employees whose first_name contains the characters 'od'.

Example - Using _ wildcard (underscore wildcard)

Next, let's explain how the _ wildcard (underscore wildcard) works in the PostgreSQL LIKE condition. Remember that _ wildcard is looking for only one character.

For example:

SELECT first_name, last_name

FROM employees

WHERE first_name LIKE 'Jas_n';

This PostgreSQL LIKE condition example would return all suppliers whose supplier_name is 5 characters long, where the first three character is 'Jas' and the last character is 'n'. For example, it could return employees whose first_name is 'Jasan', 'Jasen', 'Jasin', 'Jason', etc.

Here is another example:

SELECT *

FROM employees

WHERE employee_number LIKE '98765_';

You might find that you are looking for an account number, but you only have 5 of the 6 digits. The example above, would retrieve potentially 10 records back (where the missing value could equal anything from 0 to 9). For example, it could return employees whose employee numbers are:

987650, 987651, 987652, 987653, 987654, 987655, 987656, 987657, 987658, 987659

Example - Using NOT Operator

Next, let's look at how you would use the NOT Operator with wildcards.

Let's use the % wilcard with the NOT Operator. You could also use the PostgreSQL LIKE condition to find employees whose last_name does not start with 'J'.

For example:

SELECT first_name, last_name

FROM employees

WHERE last_name NOT LIKE 'J%';

By placing the NOT Operator in front of the PostgreSQL LIKE condition, you are able to retrieve all employees whose last_name does not start with 'J'.

Example - Using Escape Characters

It is important to understand how to "Escape Characters" when pattern matching. These examples deal specifically with escaping characters in PostgreSQL.

Let's say you wanted to search for a % or a _ character in the PostgreSQL LIKE condition. You can do this using an Escape character.

Please note that you can only define an escape character as a single character (length of 1).

For example:

SELECT *

FROM employees

WHERE last_name LIKE 'G\%';

Since we didn't specify an escape character, PostgreSQL assumes that the "\" is the escape character. PostgreSQL then assumes that the escape character is "\" which results in PostgreSQL treating the % character as a literal instead of a wildcard. This statement would then return all employees whose last_name is G%.

We can override the default escape character in PostgreSQL by providing the ESCAPE modifier as follows:

SELECT *

FROM employees

WHERE last_name LIKE 'G!%' ESCAPE '!';

This PostgreSQL LIKE condition example identifies the ! character as an escape character. The ! escape character would result in PostgreSQL treating the % character as a literal. As a result, this statement will also return all employees whose last_name is G%.

Here is another more complicated example using escape characters in the PostgreSQL LIKE condition.

SELECT *

FROM employees

WHERE last_name LIKE 'M%\%';

This PostgreSQL LIKE condition example returns all employees whose last_name starts with M and ends in %. For example, it would return a value such as 'Mathison%'. Since we did not specify an escape character in the LIKE condition, PostgreSQL assumes that the escape character is "\" which results in PostgreSQL treating the second % character as a literal instead of a wildcard.

We could modify this LIKE condition by specfying an escape character as follows:

SELECT *

FROM employees

WHERE last_name LIKE 'M%!%' ESCAPE '!';

This PostgreSQL LIKE condition example returns all employees whose last_name starts with M and ends in the literal %. For example, it would return a value such as 'Mathison%'.

You can also use the escape character with the _ character in the PostgreSQL LIKE condition.

For example:

SELECT *

FROM employees

WHERE last_name LIKE 'M%\_';

Again, since no ESCAPE modifier is provided, PostgreSQL uses "\" as the escape character resulting in the _ character to be treated as a literal instead of a wildcard. This example would then return all employees whose last_name starts with M and ends in _. For example, it would return a value such as 'Mathison_'.